﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BKH.BLL
{
    class ThuocBUO:IBUO<tbl_Thuoc>
    {
        HospitalDBDataContext context = new HospitalDBDataContext();
        public bool Insert(tbl_Thuoc obj)
        {
            try
            {
                context.tbl_Thuocs.InsertOnSubmit(obj);
                context.SubmitChanges();
                return true;

            }
            catch (Exception)
            {

                return false;
            }
        }

        public bool Delete(int id)
        {
            tbl_Thuoc item = context.tbl_Thuocs.Where(n => n.ThuocID == id).ToList<tbl_Thuoc>().FirstOrDefault();
            try
            {
                context.tbl_Thuocs.DeleteOnSubmit(item);
                context.SubmitChanges();
                return true;
            }
            catch (Exception)
            {

                return false;
            }

        }

        public bool Delete(tbl_Thuoc obj)
        {
            throw new NotImplementedException();
        }

        public List<tbl_Thuoc> GetAll()
        {
            return context.tbl_Thuocs.ToList<tbl_Thuoc>();
        }
        public List<tbl_Thuoc> GetAll(int idKhoa, String LoaiVatTu)
        {
            return context.tbl_Thuocs.Where(n => n.KhoaID == idKhoa && n.LoaiVatTu.Equals(LoaiVatTu)).ToList<tbl_Thuoc>();
        }

        public tbl_Thuoc GetSingle(int id)
        {
            throw new NotImplementedException();
        }

        public bool Update(int id, tbl_Thuoc obj)
        {
            tbl_Thuoc item = context.tbl_Thuocs.Where(n => n.ThuocID == id).ToList<tbl_Thuoc>().FirstOrDefault();
            try
            {
                item.TenThuoc = obj.TenThuoc;
                item.DonGia = obj.DonGia;
                item.SoLuong = obj.SoLuong;
                item.NgayCapNhat = obj.NgayCapNhat;
                item.NhaCungCapID = obj.NhaCungCapID;
                context.SubmitChanges();
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }
        
    }
}
